* */ class FolderPermissionsException extends Exception { } class extractor { private $_epubFilePath; private $_tempFolderPath; private $_files; private $_inflationRootPath; /** * Constructor, which takes the full path to the ePub template file. * * @param $full_path_to_epub_file * @access public */ public function __construct($full_path_to_epub_file) { $this->_epubFilePath = $full_path_to_epub_file; $this->_tempFolderPath = $this->setTempFolder(); } /** * Sets the temp folder, where the inflate operation can take place. Defaults to /tmp. * * @param $folder * @access public * @return $folder * @throws FolderPermissionsException if the folder isn't writable */ public function setTempFolder($folder = 'tmp') { if (!file_exists($folder)) { mkdir($folder, 0777, true); } if (!is_writable($folder)) { throw new FolderPermissionsException("{$folder} does not have permissions for writing"); } return $folder; } /** * Deflates a folder back into an ePub/zip file. * * @param $filename * @return mixed */ public function deflate($filename) { $files = $this->getInflatedFiles(); $zip = new ZipArchive(); if ($zip->open($filename, ZIPARCHIVE::CREATE) === true) { foreach ($files as $file) { if ($file !== $filename) { $zip->addFile($file, substr($file, strlen($this->getInflationRootPath() . '/'))); } } $zip->close(); } return $filename; } /** * Inflates the ePub template passed into the contructor. * @return bool */ public function inflate() { $this->_inflationRootPath = $this->_tempFolderPath . '/' . uniqid(); if (!file_exists($this->_inflationRootPath)) { mkdir($this->_inflationRootPath, 0777, true); } copy($this->_epubFilePath, $this->_inflationRootPath . '/epub.zip'); $zip = new ZipArchive; $res = $zip->open($this->_inflationRootPath . '/epub.zip'); if ($res === TRUE) { $zip->extractTo($this->_inflationRootPath); $zip->close(); unlink($this->_inflationRootPath . '/epub.zip'); } $this->_files = $this->directoryToArray($this->_inflationRootPath, true); return true; } /** * Returns the inflated files array. * * @return mixed */ public function getInflatedFiles() { return $this->_files; } /** * Returns the inflated root path. * * @return mixed */ public function getInflationRootPath() { return $this->_inflationRootPath; } /** * After a deflate occures, this can be called to clean up the extra folders created during the inflate. */ public function cleanup() { $dir = $this->getInflationRootPath(); $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS); $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); foreach ($files as $file) { if ($file->getFilename() === '.' || $file->getFilename() === '..') { continue; } if ($file->isDir()) { rmdir($file->getRealPath()); } else { unlink($file->getRealPath()); } } rmdir($dir); } private function directoryToArray($directory, $recursive) { $array_items = array(); if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($directory . '/' . $file)) { if ($recursive) { $array_items = array_merge($array_items, $this->directoryToArray($directory . '/' . $file, $recursive)); } //$file = $directory . '/' . $file; //$array_items[] = preg_replace("/\/\//si", "/", $file); } else { $file = $directory . '/' . $file; $array_items[] = preg_replace("/\/\//si", "/", $file); } } } closedir($handle); } return $array_items; } } ?>